﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraPrinting;
using DiaoPaiDaYin;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.样品管理.样品订单预览;
using DXApplicationTangche.UC.订单.service;
using DXApplicationTangche.UC.订单一览新.订单预览新;
using DXApplicationTangche.UC.销售台账.service;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.生产下单
{
    public partial class XtraUserControl销售日记账 : DevExpress.XtraEditors.XtraUserControl
    {
        private String shopId { get; set; }
        private Boolean is客户套号;   //  用于客户套号和补货
        private 销售排产Model model;
        public XtraUserControl销售日记账(String shopId)
        {
            InitializeComponent();
            this.shopId = shopId;
            this.判断shopId();
        }
        public XtraUserControl销售日记账(String shopId,Boolean is客户套号)
        {
            InitializeComponent();
            this.shopId = shopId;
            this.is客户套号 = is客户套号;
            this.判断shopId();
        }
        private void 判断shopId()
        {
            if ("18".Equals(this.shopId))
            {
                this.label提示.Text = "定制订单看板";
                //  按钮底层的layout
                this.layoutControlItem确认按钮.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                this.simpleButton确认.Visible = false;
                this.simpleButton确认.Enabled = false;
            }
            else if ("50".Equals(this.shopId))
            {
                this.label提示.Text = "套号订单看板";
                if (this.is客户套号)
                {
                    //  按钮底层的layout
                    this.layoutControlItem确认按钮.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    this.simpleButton确认.Visible = false;
                    this.simpleButton确认.Enabled = false;
                }
                else
                {
                    this.simpleButton确认.Visible = true;
                    this.simpleButton确认.Enabled = true;
                }
            }
            else if ("48".Equals(this.shopId))
            {
                this.label提示.Text = "样衣订单看板";
                this.simpleButton确认.Visible = true;
                this.simpleButton确认.Enabled = true;
            }
            else if ("51".Equals(this.shopId))
            {
                this.label提示.Text = "上海Luna订单看板";
                this.simpleButton确认.Visible = true;
                this.simpleButton确认.Enabled = true;
            }
            else if ("46".Equals(this.shopId))
            {
                this.label提示.Text = "日本坚山订单看板";
                this.simpleButton确认.Visible = true;
                this.simpleButton确认.Enabled = true;
            }
            else if ("49".Equals(this.shopId))
            {
                this.label提示.Text = "美国坚山订单看板";
                this.simpleButton确认.Visible = true;
                this.simpleButton确认.Enabled = true;
            }
        }

        private void dateTimePicker日期_CloseUp(object sender, EventArgs e)
        {
            this.model = new 销售排产Model(
                this.dateTimePicker日期.Value.ToString("yyyy-MM-dd"),
                this.shopId
                );
            if ("50".Equals(this.shopId))
            {
                if (this.is客户套号)
                {
                    this.model.build客户套号();
                }
                else
                {
                    this.model.build补货套号();
                }
            }
            this.gridControl销售台账.DataSource = this.model.list;
            this.bandedGridView销售台账.RefreshData();
            this.gridControl物料需求计划.DataSource = 订单Service.getOrderStatusIndexPageable(this.shopId, this.dateTimePicker日期.Value.ToString("yyyy-MM-dd"),"0","1000");
            this.gridView物料需求计划.RefreshData();
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (this.model.list.Count > 0)
            {
                //  筛选的待排产订单:订单状态为未排产
                List<销售日记账预览VO> 销售日记账预览VOs = 
                    this.model.buildXml外其他订单确认前预览().销售日记账预览VO;
                if (销售日记账预览VOs.Count > 0)
                {
                    new Form销售日记账确认预览(销售日记账预览VOs).ShowDialog();
                }
                else
                {
                    MessageBox.Show("无待排产订单");
                    return;
                }
            }
            else
            {
                MessageBox.Show("无待排产订单");
                return;
            }
            //if (this.model.list.Count > 0)
            //{
            //    //  筛选的待排产订单:订单状态为
            //    List<OrderDailyConfirmDTO> orderDailyConfirmDTOs = 
            //        this.model.buildXml外其他订单确认前筛选未排产().orderDailyConfirmDTOs;
            //    //  筛选的待排产订单是否存在判断
            //    if (orderDailyConfirmDTOs.Count <= 0)
            //    {
            //        MessageBox.Show("无待排产订单");
            //        return;
            //    }
            //    FunctionHelper.JsonSerialization(orderDailyConfirmDTOs);
            //    销售台账Service.post确认销售台账(orderDailyConfirmDTOs);
            //    MessageBox.Show("确认完成");
            //}
            //else
            //{
            //    MessageBox.Show("无待排产订单");
            //    return;
            //}
        }

        private void bandedGridView销售台账_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if ("48".Equals(this.shopId))
            {
                new Form样品订单预览新(
                    FunctionHelper.GetbandedgridViewValue(
                        this.bandedGridView销售台账,
                        this.bandedGridView销售台账.FocusedRowHandle,
                        "orderId"
                        )
                    ).ShowDialog();
            }
            else
            {
                new Form订单预览新(
                    FunctionHelper.GetbandedgridViewValue(
                        this.bandedGridView销售台账,
                        this.bandedGridView销售台账.FocusedRowHandle,
                        "orderId"
                        )
                    ).ShowDialog();
            }
        }

        private void 取消ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId = this.bandedGridView销售台账.GetRowCellValue(this.bandedGridView销售台账.FocusedRowHandle, "orderId").ToString();
                DTO返回值判断 dTO返回值判断 = 订单Service.get清单取消判断(orderId);
                if ("200".Equals(dTO返回值判断.errorCode))
                {
                    RestCall.PostHttp(URL.接口url + "/pmc/orderCancel", orderId);
                    MessageBox.Show("已改为取消订单");
                }
                else
                {
                    MessageBox.Show(dTO返回值判断.errorMessage);
                }
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

        private void simpleButton导出订单_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.gridControl销售台账.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void dockPanel1_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出物料需求计划";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                this.gridView物料需求计划.OptionsPrint.PrintDetails = true;
                this.gridView物料需求计划.OptionsPrint.ExpandAllDetails = true;
                this.gridView1.OptionsPrint.PrintDetails = true;
                this.gridView1.OptionsPrint.ExpandAllDetails = true;
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.gridView物料需求计划.ExportToXls(fileDialog.FileName, new XlsExportOptionsEx() { ExportType = DevExpress.Export.ExportType.WYSIWYG });
                DevExpress.XtraEditors.XtraMessageBox.Show("文件已保存至" + fileDialog.FileName, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }

}
